<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\model\User;
use fast\Random;
use think\Queue;

/**
 * 首页接口
 */
class Uinfo extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function getInfo()
    {
        $token = getHeaderToken();
        $tokenData = getJWT($token);
        $userModel = new User();
        $reData = $userModel->where('id',$tokenData['id'])->find();
        if(empty($reData)){
            $this->error(__('Login has expired',[],$this->lanagecode));
        }
        $this->success('ok',$reData);
    }
    public function getAvatar(){
        $data = getRandAvtar(4);
        $this->success('ok',$data);
    }
    public function setInfo(){
        $param = input();
        $token = getHeaderToken();
        $tokenData = getJWT($token);
        $userModel = new User();
        $reData = $userModel->where('id',$tokenData['id'])->find();
        if(empty($reData)){
            $this->error(__('Login has expired',[],$this->lanagecode));
        }

        $up = [];
        if (isset($param['avatar']) && !empty($param['avatar'])) {
            $host = request()->domain();
            $up['avatar'] = str_replace($host,'',$param['avatar']);
        }
        if (isset($param['nickname'])  && !empty($param['nickname'])) {
            $up['nickname'] = $param['nickname'];
            $up['username'] = $param['nickname'];
        }

        if (isset($param['mobile'])  && !empty($param['mobile'])) {
            $up['mobile'] = $param['mobile'];
        }
        if (isset($param['email'])  && !empty($param['email'])) {
            $up['email'] = $param['email'];
        }
        if (isset($param['age'])  && !empty($param['age'])) {
            $up['age'] = $param['age'];
        }
        if (isset($param['identy'])  && !empty($param['identy'])) {
            $up['identy'] = $param['identy'];
        }
        if (isset($param['birthday'])  && !empty($param['birthday'])) {
            $up['birthday'] = $param['birthday'];
            $diff_time = time()-strtotime($up['birthday']);
            if($diff_time < 0){
                $this->error(__('birthday is error',[],$this->lanagecode));
            }
            $now = new \DateTime();
            $birthDate = new \DateTime($param['birthday']);

            // 获取当前年份和月份
            $nowYear = $now->format('Y');
            $nowMonth = $now->format('m');

            // 获取出生年份和月份
            $birthYear = $birthDate->format('Y');
            $birthMonth = $birthDate->format('m');
            // 计算年份差
            $age = $nowYear - $birthYear;
            $up['age'] = $age;
        }
        $userModel = new User();
        $up['updatetime'] = time();
//        ppd($param);
        $userModel->where('id',$tokenData['id'])->update($up);
        $this->success(__('Operation completed',[],$this->lanagecode));
    }

    public function getShare(){
        $token = getHeaderToken();
        $tokenData = getJWT($token);
        if(empty($tokenData['id'])){
            $this->error('登录失效');
        }
        //找出他的推广
        $userModel = new User();
        $userData = $userModel->where('id',$tokenData['id'])->find();
        $shareData = $userModel->where('parent_id',$tokenData['id'])->select();

        if(empty($shareData)){
//            $this->error("暂无分享");
        }
        $ruleTip = '每分享5个人，成功进入小程序，则免费赠送半小时';
        if($userData['group_id'] == 4){
            $yongjin = db('user_group')->where('id',$userData['group_id'])->value('yongjin');
            $ruleTip = '每推广出去的用户，成功购买设备，您将获得'.$yongjin."%的佣金";
        }
        $this->success('ok',compact('userData','shareData','ruleTip'));
    }

    /**
     * 修改密码
     */
    public function changepwd()
    {
        $token = getHeaderToken();
        $tokenData = getJWT($token);
        $user_id = $tokenData['id'];
        $reData = db('user')->where('id',$tokenData['id'])->find();
        if(empty($reData)){
            $this->error(__('Login has expired',[],$this->lanagecode));
        }

        $params = input();
        $oldpassword = $params["oldpassword"];
        $newpassword = $params["newpassword"];
        $renewpassword = $params["renewpassword"];

        if($renewpassword != $newpassword){
            $this->error(__("Password and confirm password don't match",[],$this->lanagecode));
        }


        $pwd_mcript = $this->auth->getEncryptPassword($oldpassword,$reData['salt']);
        if($pwd_mcript != $reData['password']){
            $this->error(__('Password is incorrect',[],$this->lanagecode));
        }

        $salt = Random::alnum();
        $newpassword = $this->auth->getEncryptPassword($newpassword, $salt);

        db('user')->where('id',$reData['id'])->update(['loginfailure' => 0, 'password' => $newpassword, 'salt' => $salt]);

        $this->success(__('Chanage password successful',[],$this->lanagecode));
    }
}
